#include "preprocesamiento.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

void preprocesar(Mat src, Mat &dst,int cannyThresh ){
	Mat aux = src.clone();
	dst = src.clone();
	Mat aux2;
	equalizeHist( aux, aux );
	if (cannyThresh < 20) cannyThresh = 20;

	blur(aux, dst, Size(3, 3));
	Canny(dst, dst, cannyThresh/2, cannyThresh*2.5 , 3);
	dilate(dst, dst, Mat(), Point(-1,-1));
	//erode(dst,dst,Mat(), Point(-1,-1));
};
void preprocesar(Mat src, Mat &dst,int cannyThresh1,int cannyThresh2){
	dst = src.clone();
	blur(dst, dst, Size(3, 3));
	Canny(dst, dst, cannyThresh1, cannyThresh2 , 3);
	dilate(dst, dst, Mat(), Point(-1,-1));
};

void preprocesarPasoaPaso(Mat src, Mat &dst,int cannyThresh1,int cannyThresh2,int paso){
	int cont = paso;
	dst = src.clone();
	if (cont > 0 )
		blur(dst, dst, Size(3, 3));
	if (cont > 1 )
		Canny(dst, dst, cannyThresh1, cannyThresh2 , 3);
	if (cont > 2 )
		dilate(dst, dst, Mat(), Point(-1,-1));
};
void preprocesarPlaca(Mat src, Mat &dst ){
	Mat auxBGR = src.clone();

	Mat channelBGR[3];
	split(auxBGR, channelBGR);
	Mat aux2 = Mat::zeros(src.size(), CV_8UC1);

	for (int i = 0; i < src.cols*src.rows; i++){
		int b = channelBGR[2].data[i], g = channelBGR[1].data[i], r = channelBGR[0].data[i];
		if ( g > r && g > b && r < 100 && b < 100 && g < 130){
			channelBGR[2].data[i] = (r*1.2 > 255 ? 255 : r*1.2);
			channelBGR[1].data[i] = (g*1.2 > 255 ? 255 : g*1.2);
			channelBGR[0].data[i] = (b*1.2 > 255 ? 255 : b*1.2);
		}
	}
	merge(channelBGR,3,aux2);
	cvtColor(aux2, dst , CV_BGR2GRAY);
	threshold(dst, dst, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
};
void preprocesarPlaca(Mat src, Mat &dst,int thresh ){
	//cvtColor(src, dst , CV_BGR2GRAY);
	dst = src.clone();
	threshold(dst, dst, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
};